//
//  CommonChoseTypeRoleView.m
//  DouLinBusiness
//
//  Created by ypj on 2021/4/13.
//

#import "CommonChoseTypeRoleView.h"

@implementation CommonChoseTypeRoleView
-(void)awakeFromNib{
    [super awakeFromNib];
     self.backgroundColor=[UIColor clearColor];
    [self.blackView whenTapped:^{
        [self removeFromSuperview];
    }];
     self.tableView.delegate = self;
     self.tableView.dataSource = self;
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView createBordersWithColor:[UIColor whiteColor] withCornerRadius:0 andWidth:0.5];
    [self.tableView registerClass:[ChoseTableViewCell class] forCellReuseIdentifier:[ChoseTableViewCell reuseIdentifier]];
 }
+ (instancetype)shareInstance{
    return [[NSBundle mainBundle] loadNibNamed:@"CommonChoseTypeRoleView" owner:nil options:nil][0];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChoseTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:[ChoseTableViewCell reuseIdentifier]];
    if(!cell){
        cell = [[ChoseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[ChoseTableViewCell reuseIdentifier]];
    }
    //    if ([self.typeValue isEqualToString:@"1"]){
    JueSeModel *model = self.dataSource[indexPath.row];
    if ([NSString isNUllString:model.value]) {
        [cell.choseButtonLb setTitle:model.roleName forState:UIControlStateNormal];
    } else {
        [cell.choseButtonLb setTitle:model.value forState:UIControlStateNormal];
    }
    cell.choseButtonLb.tag = indexPath.row;
    if ([self.selectTitle isEqualToString:model.roleName] || [self.selectTitle isEqualToString:model.value]){
        [cell.choseButtonLb setTitleColor:mainColor forState:UIControlStateNormal];
    } else {
        [cell.choseButtonLb setTitleColor:Black_1A1A1A forState:UIControlStateNormal];
    }
//    }else if([self.typeValue isEqualToString:@"3"]){
////        CategoryModel  *   model=self.dataSource[indexPath.row];
////        [cell.choseButtonLb setTitle:model.name forState:UIControlStateNormal];
////         cell.choseButtonLb.tag=indexPath.row;
////        if ([self.selectTitle isEqualToString:model.name]){
////            [cell.choseButtonLb setTitleColor:mainColor forState:UIControlStateNormal];
////        } else {
////            [cell.choseButtonLb setTitleColor:Black_1A1A1A forState:UIControlStateNormal];
////        }
//    } else {
//        NSString *nameString = self.dataSource[indexPath.row];
//        [cell.choseButtonLb setTitle:nameString forState:UIControlStateNormal];
//         cell.choseButtonLb.tag = indexPath.row;
//        if ([self.selectTitle isEqualToString:nameString]){
//            [cell.choseButtonLb setTitleColor:mainColor forState:UIControlStateNormal];
//        } else {
//            [cell.choseButtonLb setTitleColor:Black_1A1A1A forState:UIControlStateNormal];
//        }
//     }
     return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [ChoseTableViewCell cellHeight];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 30)];
    UILabel *titleLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 30)];
    titleLb.text = self.titleString;
    titleLb.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:titleLb];
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.typeValue isEqualToString:@"1"]){
        JueSeModel *model = self.dataSource[indexPath.row];
        self.ToSendValue(model);
    }else if([self.typeValue isEqualToString:@"3"]){
//        CategoryModel  *   model=self.dataSource[indexPath.row];
//        self.ToSendCatrgoryValue(model);
    } else {
        NSString *nameString=self.dataSource[indexPath.row];
        self.ToSendStringValue(nameString);
    }
}

-(void)DataSourceToShowAlert:(NSMutableArray *)dataSource withSelectTitle:(NSString *)titleString withType:(NSString *)type withTitle:(NSString *)alertTitle{
    self.dataSource = dataSource;
    self.selectTitle = titleString;
    self.titleString = alertTitle;
    self.typeValue = type;
    self.bgTopHeight.constant = B_HEIGHT-dataSource.count * 40 - 30 - LL_TabbarSafeBottomMargin - mcTopBarSafeHeight;
    self.blackView.frame = CGRectMake(0, 0, ScreenWidth, B_HEIGHT);
    self.bgView.frame = CGRectMake(0, B_HEIGHT-dataSource.count*40-30, ScreenWidth, dataSource.count * 40 + 30 + LL_TabbarSafeBottomMargin);
    self.tableView.frame = CGRectMake(0, 0, ScreenWidth, dataSource.count*40+30+LL_TabbarSafeBottomMargin);
   [self.tableView reloadData];

}

@end
